/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import RMS.ScoreRMS;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class HighScore extends Canvas {

    private Image bufferImage, highScore;
    private Graphics g;
    private Font f;
    private Start midlet;
    private int instanceKeyCode;

    public HighScore(Start midlet) {
        this.setFullScreenMode(true);
        bufferImage = Image.createImage(getWidth(), getHeight());
        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        try {
            highScore = Image.createImage("/imgs/highscore.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        g = bufferImage.getGraphics();
        g.setFont(f);
        this.midlet = midlet;
    }

    protected void paint(Graphics g) {
        this.setFullScreenMode(true);
        render();
        getScore();
        g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    public void render() {
        g.setColor(0x00FAC012);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x00684972);
        g.fillRoundRect(getWidth() / 7, getHeight() / 8, getWidth() * 5 / 7, getHeight() * 5 / 8, 5, 5);
        g.setColor(0x00F8E660);
        g.fillRoundRect(getWidth() / 7 + 5, getHeight() / 8 + 5, getWidth() * 5 / 7 - 10,
                getHeight() * 5 / 8 - 10, 5, 5);
        g.drawImage(highScore, getWidth() / 2, getHeight() / 8, Graphics.HCENTER | Graphics.BOTTOM);
        g.setColor(0x00DD6F00);
        g.fillRect(0, getHeight() - f.getHeight() - 2, getWidth(), f.getHeight() + 2);

        g.setColor(0xffffff);
        g.drawString("Thoát", 2, getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM);
        g.drawLine(0, getHeight() - f.getHeight() - 3, getWidth(), getHeight() - f.getHeight() - 3);
    }

    private void getScore() {
        ScoreRMS scoreRMS = new ScoreRMS();
        scoreRMS.getNameNscore();
        String[] name = new String[5];
        name = scoreRMS.getNames();
        int[] score = new int[5];
        score = scoreRMS.getScores();
        for (int i = 0; i < 5; i++) {
            if (name[i] != null) {
                if (i == 0) {
                    g.setColor(255, 0, 0);

                } else {
                    g.setColor(0, 0, 255);

                }
                g.drawString(name[i], getWidth() / 7 + 7, getHeight() / 8 + 9 + i * (f.getHeight() + 4),
                        Graphics.LEFT | Graphics.TOP);
                g.drawString(String.valueOf(score[i]), getWidth() * 6 / 7 - 5, getHeight() / 8 + 9 + i * (f.getHeight() + 4),
                        Graphics.RIGHT | Graphics.TOP);
                g.setColor(0, 255, 0);
                g.drawLine(getWidth() / 7 + 5, getHeight() / 8 + 9 + (i + 1) * (f.getHeight() + 4),
                        getWidth() * 6 / 7 - 5, getHeight() / 8 + 9 + (i + 1) * (f.getHeight() + 4));
            }
        }
    }

    protected void pointerPressed(int x, int y) {
        if (x <= getWidth() && x >= 0) {
            if (y <= getHeight() && y >= 0) {
                highScore = null;
                bufferImage = null;
                System.gc();
                midlet.disManager.home();
            }
        }
    }

    protected void keyPressed(int keyCode) {
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);
        switch (instanceKeyCode) {
            case KeyCodeAdapter.SOFT_KEY_LEFT:
            case KeyCodeAdapter.CENTER_KEY:
            case KeyCodeAdapter.KEY_5:
                highScore = null;
                bufferImage = null;
                System.gc();
                midlet.disManager.home();
        }
    }
}
